/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.support;

import com.jspx.boot.EnvFactory;
import com.jspx.boot.environment.Environment;
import com.jspx.boot.environment.EnvironmentTemplate;
import com.jspx.txweb.annotation.HttpMethod;
import com.jspx.txweb.annotation.Safety;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.txweb.validator.DataTypeValidator;
import com.jspx.txweb.validator.Validator;
import com.jspx.utils.FileUtil;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-5-19
 * Time: 22:29:15
 * com.jspx.validator.view.ValidatorView     DataTypeValidator
 * 后期计划:添加 db保存，和界面设计支持
 */
@HttpMethod(caption="数据验证")
public class ValidatorAction extends ActionSupport {
    final private static EnvironmentTemplate environmentTemplate = EnvFactory.getEnvironmentTemplate();
    final private static Validator processor = new DataTypeValidator();
    private String type = "json";

    public ValidatorAction() {

    }

    @Safety(request = false)
    public void setConfigFile(String configFile) {
        processor.setConfigFile(configFile);
    }

    public String getConfigFile() {
        return processor.getConfigFile();
    }

    @Safety(length = 220,level = 2)
    public void setFormId(String formId) {
        if (formId.contains("/")) formId = FileUtil.getFileNamePart(formId);
        processor.setFormId(formId);
    }

    public String getFormId() {
        return processor.getFormId();
    }

    public String getXML() throws Exception
    {
        return processor.getXML();
    }

    public String getJson() throws Exception
    {
        return processor.getJson();
    }

    public Validator getProcessor() {
        return processor;
    }

    public String getType() {
        return type;
    }

    @Safety(length = 20,level = 2)
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String execute() throws Exception
    {
        processor.setEncode(environmentTemplate.getString(Environment.encode,Environment.defaultEncode));
        if ("json".equalsIgnoreCase(type))
        {
            TXWebUtil.print(getJson(),TXWebUtil.jsonType,response);
        } else {
            TXWebUtil.print("<?xml version=\"1.0\" encoding=\"" + processor.getEncode() + "\"?>\r\n"+getXML(),TXWebUtil.xmlType,response);
        }
        return NONE;
    }
}